home *** CD-ROM | disk | FTP | other *** search
/ Aminet 2 / Aminet AMIGA CDROM (1994)(Walnut Creek)[Feb 1994][W.O. 44790-1].iso / Aminet / util / misc / easyproc.lha / EasyProcess / Source / Dragon / Dragon.c next >
Encoding:
C/C++ Source or Header  |  1992-09-08  |  5.8 KB  |  340 lines

  1. #include <arpbase.h>
  2. #include <arp_proto.h>
  3. #include "Dragon.h"
  4. #include "DragonGad.h"
  5. #include "source:launch/launch.h"
  6.  
  7.  
  8. struct FileRequester *FRequest;
  9. struct Window *DragonWin, *InputWin;
  10. struct Screen *Screen;
  11. struct ArpBase *ArpBase;
  12. struct IntuitionBase *IntuitionBase;
  13. struct GfxBase *GfxBase;
  14.  
  15.  
  16. void main (void)
  17. {
  18.     struct IntuiMessage *Message;
  19.     struct Gadget *Gadget;
  20.     LONG Class;
  21.     WORD StartX, StartY;
  22.     struct Dragon Drag, *NewDrag;
  23.  
  24.     SetUp ();
  25.  
  26.     CopyMem (DragonWin->RPort, &Drag.RastPort, sizeof (struct RastPort));
  27.     Drag.DuplicateCount = 0;
  28.     Drag.KindCount = 0;
  29.  
  30.     while (1)
  31.     {
  32.         WaitPort (InputWin->UserPort);
  33.  
  34.         while ( (Message = (struct IntuiMessage *)GetMsg (InputWin->UserPort)) != NULL)
  35.         {
  36.             Class = Message->Class;
  37.             Gadget = (struct Gadget *)Message->IAddress;
  38.             ReplyMsg (Message);
  39.  
  40.             if (Class == CLOSEWINDOW)
  41.             {
  42.                 CleanUp ();
  43.             }
  44.  
  45.             if (Class == GADGETUP)
  46.             {
  47.                 switch (Gadget->GadgetID)
  48.                 {
  49.                     case DAYGAD:
  50.                     {
  51.                         Drag.Day = ReadNumber (DayGadSIBuff, NULL);
  52.                         ActivateGadget (&XGad, InputWin, 0);
  53.                         break;
  54.                     }
  55.  
  56.                     case CELLGAD:
  57.                     {
  58.                         Drag.Cell = ReadNumber (CellGadSIBuff, NULL);
  59.                         ActivateGadget (&DayGad, InputWin, 0);
  60.                         break;
  61.                     }
  62.  
  63.                     case XGAD:
  64.                     {
  65.                         StartX = ReadNumber (XGadSIBuff, NULL);
  66.                         ActivateGadget (&YGad, InputWin, 0);
  67.                         break;
  68.                     }
  69.  
  70.                     case YGAD:
  71.                     {
  72.                         StartY = ReadNumber (YGadSIBuff, NULL);
  73.                         ActivateGadget (&CellGad, InputWin, 0);
  74.                         break;
  75.                     }
  76.  
  77.                     case LOADGAD:
  78.                     {
  79.                         if (Load (&Drag) != 0)
  80.                         {
  81.                             Drag.DuplicateCount = 0;
  82.                             Drag.KindCount = 0;
  83.                         }
  84.                         ActivateGadget (&DayGad, InputWin, 0);
  85.                         break;
  86.                     }
  87.  
  88.                     case GOGAD:
  89.                     {
  90.                         Drag.DragonX = StartX;
  91.                         Drag.DragonY = StartY;
  92.                         NewDrag = AllocMem (sizeof (struct Dragon), 0L);
  93.                         if (NewDrag)
  94.                         {
  95.                             CopyMem (&Drag, NewDrag, sizeof (struct Dragon));
  96.                             if (NULL == StartProcess ("Dragon generator", (CPTR)MakeDragon, NewDrag, -1))
  97.                             {
  98.                                 DisplayBeep (Screen);
  99.                                 FreeMem (NewDrag, sizeof (struct Dragon));
  100.                             }
  101.                         }
  102.                         ActivateGadget (&DayGad, InputWin, 0);
  103.                         break;
  104.                     }
  105.  
  106.                     case STOPGAD:
  107.                     {
  108.                         KillProcesses ();
  109.                         WaitProcesses ();
  110.                         ActivateGadget (&DayGad, InputWin, 0);
  111.                         break;
  112.                     }
  113.  
  114.                     case CLEARGAD:
  115.                     {
  116.                         SetAPen (&Drag.RastPort, 0);
  117.                         RectFill (&Drag.RastPort, 0, 0, 640, 399);
  118.                         SetAPen (&Drag.RastPort, 1);
  119.                         ActivateGadget (&DayGad, InputWin, 0);
  120.                         break;
  121.                     }
  122.                 }
  123.             }
  124.         }
  125.     }
  126. }
  127.  
  128.  
  129. void MakeDragon (struct Dragon *Drag)
  130. {
  131.     Move (&Drag->RastPort, Drag->DragonX, Drag->DragonY);
  132.     Dragon (Drag->Day, Drag->Cell, Drag);
  133.     FreeMem (Drag, sizeof (struct Dragon));
  134. }
  135.  
  136.  
  137. void Dragon (WORD Day, WORD Cell, struct Dragon *Drag)
  138. {
  139.     WORD i;
  140.  
  141.     if (CheckKill ())
  142.     {
  143.         return;
  144.     }
  145.  
  146.     if (Day <= 0)
  147.     {
  148.         Drag->DragonX += Drag->MoveX[Cell];
  149.         Drag->DragonY += Drag->MoveY[Cell];
  150.         Draw (&Drag->RastPort, Drag->DragonX, Drag->DragonY);
  151.     }
  152.     else
  153.     {
  154.         for (i = 0; i < Drag->DuplicateCount; i++)
  155.         {
  156.             Dragon (Day-1, Drag->Duplicate[Cell][i], Drag);
  157.         }
  158.     }
  159. }
  160.  
  161.  
  162. WORD ReadNumber (BYTE *Buffer, WORD *PosPtr)
  163. {
  164.     WORD Number, Sign, Position;
  165.  
  166.     Sign = 1;
  167.     Position = 0;
  168.     Number = 0;
  169.  
  170.     while (*Buffer == ' ' || *Buffer == 10)
  171.     {
  172.         Position++;
  173.         Buffer++;
  174.     }
  175.  
  176.     if (*Buffer == '-')
  177.     {
  178.         Sign = -1;
  179.         Position++;
  180.         Buffer++;
  181.     }
  182.  
  183.     while (*Buffer >= '0' && *Buffer <= '9')
  184.     {
  185.         Number *= 10;
  186.         Number += *Buffer - '0';
  187.         Buffer++;
  188.         Position++;
  189.     }
  190.  
  191.     if (PosPtr != NULL)
  192.     {
  193.         *PosPtr += Position;
  194.     }
  195.  
  196.     return (Sign == 1 ? Number : -Number);
  197. }
  198.  
  199.  
  200. WORD Load (struct Dragon *Drag)
  201. {
  202.     LONG Len;
  203.     WORD i,j;
  204.     WORD Position;
  205.     BYTE *Buffer, Name[256];
  206.     BPTR File;
  207.  
  208.     if (FileRequest (FRequest))
  209.     {
  210.         strcpy (Name, FRequest->fr_Dir);
  211.         TackOn (Name, FRequest->fr_File);
  212.  
  213.         if ( (File = Open (Name, MODE_OLDFILE)) == NULL)
  214.             return 1;
  215.  
  216.         Len = Seek (File, 0, OFFSET_END);
  217.         Len = Seek (File, 0, OFFSET_BEGINNING);
  218.  
  219.         if ( (Buffer = DosAllocMem (Len)) == NULL)
  220.         {
  221.             Close (File);
  222.             return 1;
  223.         }
  224.  
  225.         Read (File, Buffer, Len);
  226.         Close (File);
  227.  
  228.         Position = 0;
  229.  
  230.         Drag->KindCount = ReadNumber (Buffer+Position, &Position);
  231.         Drag->DuplicateCount = ReadNumber (Buffer+Position, &Position);
  232.  
  233.         if (Drag->KindCount <= 0 || Drag->DuplicateCount <= 0)
  234.             return 1;
  235.  
  236.         for (i = 0; i < Drag->KindCount; i++)
  237.         {
  238.             Drag->MoveX[i] = ReadNumber (Buffer+Position, &Position);
  239.         }
  240.  
  241.         for (i = 0; i < Drag->KindCount; i++)
  242.         {
  243.             Drag->MoveY[i] = ReadNumber (Buffer+Position, &Position);
  244.         }
  245.  
  246.         for (i = 0; i < Drag->KindCount; i++)
  247.         {
  248.             for (j = 0; j < Drag->DuplicateCount; j++)
  249.             {
  250.                 Drag->Duplicate[i][j] = ReadNumber (Buffer+Position, &Position);
  251.                 if (Drag->Duplicate[i][j] < 0)
  252.                 {
  253.                     return 1;
  254.                 }
  255.             }
  256.         }
  257.  
  258.         DosFreeMem (Buffer);
  259.     }
  260.  
  261.     return 0;
  262. }
  263.  
  264.  
  265. void SetUp (void)
  266. {
  267.     if ( (ArpBase = (void *)OpenLibrary ("arp.library", 0)) == NULL)
  268.     {
  269.         CleanUp ();
  270.     }
  271.  
  272.     if ( (GfxBase = (void *)OpenLibrary ("graphics.library", 0)) == NULL)
  273.     {
  274.         CleanUp ();
  275.     }
  276.  
  277.     if ( (IntuitionBase = (void *)OpenLibrary ("intuition.library", 0)) == NULL)
  278.     {
  279.         CleanUp ();
  280.     }
  281.  
  282.     if ( (Screen = OpenScreen(&DragonNewScreen)) == NULL )
  283.     {
  284.         CleanUp();
  285.     }
  286.  
  287.     SetRGB4 (&Screen->ViewPort, 0, 0, 0, 0);
  288.     SetRGB4 (&Screen->ViewPort, 1, 4, 6, 14);
  289.  
  290.     DragonNewWin.Screen = InputNewWin.Screen = Screen;
  291.  
  292.     if ( (DragonWin = OpenWindow (&DragonNewWin)) == NULL )
  293.     {
  294.         CleanUp ();
  295.     }
  296.  
  297.     if ( (InputWin = OpenWindow (&InputNewWin)) == NULL )
  298.     {
  299.         CleanUp ();
  300.     }
  301.  
  302.     FRequest = ArpAllocFreq ();
  303.     if (FRequest)
  304.     {
  305.         FRequest->fr_Window = InputWin;
  306.         FRequest->fr_Hail = "Choose Dragon...";
  307.     }
  308.     else
  309.     {
  310.         CleanUp ();
  311.     }
  312. }
  313.  
  314.  
  315. void CleanUp (void)
  316. {
  317.     KillProcesses ();
  318.     WaitProcesses ();
  319.  
  320.     if (InputWin)
  321.         CloseWindow (InputWin);
  322.  
  323.     if (DragonWin)
  324.         CloseWindow (DragonWin);
  325.  
  326.     if (Screen)
  327.         CloseScreen (Screen);
  328.  
  329.     if (IntuitionBase)
  330.         CloseLibrary (IntuitionBase);
  331.  
  332.     if (GfxBase)
  333.         CloseLibrary (GfxBase);
  334.  
  335.     if (ArpBase)
  336.         CloseLibrary (ArpBase);
  337.  
  338.     exit (0);
  339. }
  340.